package com.dandelion.common.service;

import com.dandelion.common.dao.auto.mapper.UserAccountMapper;
import com.dandelion.common.dao.auto.pojo.UserAccount;
import com.dandelion.common.domain.WxUser;
import com.dandelion.common.exception.UserAccountNotExistsException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserAccountService {
    private static final Logger logger = LoggerFactory.getLogger(UserAccountService.class);

    @Autowired
    private UserAccountMapper userAccountMapper;

    public UserAccount createNewUserAccount(WxUser user) {
        UserAccount account = new UserAccount();
        account.setNickname(StringUtils.EMPTY);
        account.setAvatar(StringUtils.EMPTY);
        account.setCreateTime(new Date());
        userAccountMapper.insertSelective(account);
        return account;
    }

    public UserAccount getAccountById(long userId) throws UserAccountNotExistsException {
        UserAccount account = userAccountMapper.selectByPrimaryKey(userId);
        if (account == null) {
            throw new UserAccountNotExistsException("该用户不存在");
        }
        return account;
    }
}
